// pages/userauthor/userauthor.js
var range, range3, pages, prevPage;
var app = getApp();
var sceneres = {}
var winScale = 750 / app.data.deviceInfo.windowWidth
var shihan = require('../../common/fetch')
var config = require('../../common/config')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    banners: '',
    getPhoneCode: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) { //get_adv_view
    var that = this;
    console.log(options)
    config.webresiter = options.webresiter || 0;
    pages = getCurrentPages();
    if (pages.length >= 2) {
      prevPage = pages[pages.length - 2];
      prevPage.setData({
        jumpFlag: true
      })
    }
    //获取页面展示图片
    shihan.sendRequest(config.get_adv_view + 123 + '.html', 'GET', {})
      .then(function(response) {
        if (response.data.code == 0) {
          that.setData({
            banners: response.data.data
          })
        }
      }, function(error) {});
  },
  getPhoneNumber: function(e) {

    var that = this;
    if (!e.detail.encryptedData) {
      console.log('用户拒绝获取手机号');
      return;
    }

    var data = {};
    data["channel_id"] = config.cid;
    data["partner_id"] = config.partner_id;
    data["iv"] = e.detail.iv;
    data["encrypted_data"] = e.detail.encryptedData;
    data["phone_code"] = that.data.getPhoneCode; //解析手机号用的code

    // console.log(data);

    shihan.sendRequest(config.wechatLogin, 'POST', data)
      .then(function(response) {
        if (response.data.code == 0) {

          wx.setStorageSync('customerId', response.data.data.sh_customer_id); //用户ID
          wx.setStorageSync('open_id', response.data.data.sh_open_id); //open_id
          wx.setStorageSync('token', response.data.data.sh_token); //用户登录凭证token

          that.registerJump()

        } else {
          that.onShow(); //绑定失败后需要重新刷新code
          wx.showModal({
            title: '提示',
            content: response.data.msg,
            confirmText: "确认",
            confirmColor: "#c40000",
            showCancel: false
          })
        }
      }, function(error) {});
  },

  registerJump: function() {
    if (config.webresiter || config.specialpage) {
      config.specialpage = 0
      var token = wx.getStorageSync('token');
      var open_id = wx.getStorageSync('open_id');
      if (prevPage.data.weburl&&prevPage.data.weburl.indexOf('?') > 0) {
        prevPage.setData({
          weburl: prevPage.data.weburl + '&token=' + token + '&device=' + open_id
        })
      } else {
        prevPage.setData({
          weburl: prevPage.data.weburl + '?token=' + token + '&device=' + open_id
        })
      }
    }
    setTimeout(function() {
      wx.navigateBack({})
    }, 300)
  },

  inputphone: function() {
    wx.navigateTo({
      url: '/pages/bindpage/bindpage'
    })
  },
  jumpadvert: function(e) { //广告位跳转
    app.AdSkipper(e)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    wx.login({
      success: function(res) {
        that.setData({
          'getPhoneCode': res.code
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {}
})